package com.springboot.rigger;

import java.util.Scanner;

public class TaxCalculator {

    // 常量定义 税率3%，增值税 = 结算金额 / (1+3%) x 3%
    private static final double VAT_RATE = 0.03;
    private static final double SURTAX_RATE = 0.10;
    // 若（结算金额-增值税-附加税）不超过800元，不用缴纳个人所得税；
    private static final double THRESHOLD_NO_INCOME_TAX = 800.0;
    // 若（结算金额-增值税-附加税）不超过4000元，应纳税所得额 = (结算金额-增值税-附加税)-800元；
    private static final double THRESHOLD_LOWER_INCOME_TAX_BRACKET = 4000.0;

    // 个人所得税税率和速算扣除数
    private static final double[][] INCOME_TAX_BRACKETS = {{20000, 0.20, 0}, {50000, 0.30, 2000}, {Double.MAX_VALUE, 0.40, 7000}};

    public static void main(String[] args) {
        // 示例结算金额
        Scanner scanner = new Scanner(System.in);

        // 提示用户输入结算金额
        System.out.print("请输入结算金额: ");
        double settlementAmount = scanner.nextDouble();

        // 计算税后收入
        double afterTaxIncome = calculateAfterTaxIncome(settlementAmount);

        // 输出结果
        System.out.println("税后收入: 结算金额-个人所得税-增值税-附加税 = " + String.format("%.2f", afterTaxIncome));

        // 关闭扫描器
        scanner.close();
    }
    /*
    *   若（结算金额-增值税-附加税）不超过800元，不用缴纳个人所得税
        若（结算金额-增值税-附加税）不超过4000元，应纳税所得额 = (结算金额-增值税-附加税)-800元
        若（结算金额-增值税-附加税）超过4000元，应纳税所得额=(结算金额-增值税-附加税)x (1-20%）
    * */

    public static double calculateAfterTaxIncome(double settlementAmount) {
        // 计算增值税
        double vat = settlementAmount / (1 + VAT_RATE) * VAT_RATE;
        System.out.println("增值税计算为：" + String.format("%.2f", vat));
        // 计算附加税
        double surtax = vat * SURTAX_RATE;
        System.out.println("附加税计算为：" + String.format("%.2f", surtax));
        // 计算结算金额
        double taxableIncome = settlementAmount - vat - surtax;
        System.out.println("结算金额为：" + String.format("%.2f", taxableIncome));
        double incomeTax = 0.0;

        if (taxableIncome > THRESHOLD_NO_INCOME_TAX) {
            // 若（结算金额-增值税-附加税）不超过800元，不用缴纳个人所得税；
            if (taxableIncome <= THRESHOLD_LOWER_INCOME_TAX_BRACKET) {
                incomeTax = (taxableIncome - THRESHOLD_NO_INCOME_TAX) * getTaxRateAndQuickDeduction(taxableIncome).rate - getTaxRateAndQuickDeduction(taxableIncome).quickDeduction;
            } else {
                incomeTax = taxableIncome * 0.80 * getTaxRateAndQuickDeduction(taxableIncome * 0.80).rate - getTaxRateAndQuickDeduction(taxableIncome * 0.80).quickDeduction;
            }
        }
        System.out.println("个人所得税金额为：" + incomeTax);

        // 计算税后收入 结算金额-增值税-附加税-个人所得税（不超过20000的扣0%）
        double afterTaxIncome = settlementAmount - vat - surtax - incomeTax;

        return afterTaxIncome;
    }

    // 根据应纳税所得额获取税率和速算扣除数
    private static TaxRateAndQuickDeduction getTaxRateAndQuickDeduction(double taxableIncome) {
        for (double[] bracket : INCOME_TAX_BRACKETS) {
            if (taxableIncome <= bracket[0]) {
                return new TaxRateAndQuickDeduction(bracket[1], bracket[2]);
            }
        }
        // 默认返回最后一个税率和速算扣除数（理论上不会发生）
        return new TaxRateAndQuickDeduction(INCOME_TAX_BRACKETS[INCOME_TAX_BRACKETS.length - 1][1], INCOME_TAX_BRACKETS[INCOME_TAX_BRACKETS.length - 1][2]);
    }

    // 内部类，用于存储税率和速算扣除数
    private static class TaxRateAndQuickDeduction {
        double rate;
        double quickDeduction;

        TaxRateAndQuickDeduction(double rate, double quickDeduction) {
            this.rate = rate;
            this.quickDeduction = quickDeduction;
        }
    }
}